<?php
	define('BDD_DSN', 'mysql:dbname=cdg;host=localhost'); // DSN BDD
	define('BDD_UTILISATEUR', 'root'); // LOGIN BDD
	define('BDD_MOT_DE_PASSE', ''); // MOT DE PASSE BDD
	define('BDD_HOST', 'localhost'); // SERVEUR BDD
	define('BDD_BASE', 'cdg'); // BASE BDD
	define('DIR_LIB', 'lib/');
	define('DIR_UPLOAD', 'xls/');
	define('DIR_LOGS', 'logs/');
	define('DIR_CACHE', 'cache/');
	define('DIR_TMP', 'tmp/');
	define('DIR_TRAD', 'trad/');
	define('DIR_IMG', 'img/');
	
	include_once('../../lib/class.fonctions.php');
	include_once('../../lib/class.cache.php');
	include_once('../../lib/class.db.php');
	include_once('../../lib/class.debug.php');
	include_once('../../lib/class.activerecord.php');
	include_once('../../lib/class.secure.php');
	
	// On instancie les classes principales
	$gp_fonc = GP_fonctions::getInstance();
	$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
	$gp_request = GP_secure::getInstance();
	
	function get_fields($table)
	{
		global $gp_db;
		
		$retour = array();
		
		$q = "DESCRIBE " . $table;
		$res = $gp_db -> fetchArray($q);
		foreach ($res as $f => $tab)
		{
			foreach ($tab as $c => $v)
			{
				if ($c == 'Field' && $v <> 'id' && $v <> 'datecrea' && $v <> 'datemodif')
					$retour[] = $v;
			}
		}
		
		return $retour;
	}
	
	function makecrud($p)
	{
		$champstpl = "\t\t" .'$retour["##champ##"]["id"] = "##champ##";
		$retour["##champ##"]["Affichage"] = "##affichage##";
		$retour["##champ##"]["type"] = "##type##";
		##select##
		##premierchoix##
		$retour["##champ##"]["defaut"] = "##defaut##";
		$retour["##champ##"]["js"] = "##js##";
		$retour["##champ##"]["obligatoire"] = ##obligatoire##;
		$retour["##champ##"]["tri"] = ##tri##;
		$retour["##champ##"]["filtre"] = ##filtre##;';
		
		$champs_ajout = '';
		$champs_dup = '';
		$champs_maj = '';
		$champs_liste = '';
		$champs_vue = '';
		
		$nums = array();
		foreach ($p as $c => $v)
		{
			if (strstr($c, 'id_'))
			{
				list($dummy, $nume) = explode('id_', $c, 2);
				if (!in_array($nume, $nums))
					$nums[] = $nume;
			}
		}
		
		$tri_defaut = '';
		
		foreach ($nums as $num)
		{
			$tpl = $champstpl;
			
			$champ = $p['id_' . $num];
			$affichage = $p['affichage_' . $num];
			$defaut = $p['defaut_' . $num];
			$type = $p['type_' . $num];
			$js = $p['js_' . $num];
			$tri = $p['tri_' . $num];
			$filtre = $p['filtre_' . $num];
			$obligatoire = $p['obligatoire_' . $num];
			$presentajout = $p['presentajout_' . $num];
			$presentdup = $p['presentdup_' . $num];
			$presentmaj = $p['presentmaj_' . $num];
			$presentliste = $p['presentliste_' . $num];
			$presentvue = $p['presentvue_' . $num];
			$premierchoix = $p['premierchoix_' . $num];
			$defauttri = $p['defauttri_' . $num];
			
			if ($defauttri == 'true')
				$tri_defaut = $champ;
			
			if (strlen($premierchoix) > 0)
				$tpl = str_replace('##premierchoix##', '$retour["'.$champ.'"]["premierchoix"] = '.$premierchoix.' . "%%";', $tpl);
			else
				$tpl = str_replace('##premierchoix##', '', $tpl);
				
			if ($type == 'select' || $type == 'chackbox' || $type == 'radio')
			{
				$typeselect = $p['typeselect_' . $num];
				$data = $p['data_' . $num];
				$tpl = str_replace('##select##', '$retour["'.$champ.'"]["typeselect"] = "'.$typeselect.'";' . "\n\t\t" . '$retour["'.$champ.'"]["data"] = "'.$data.'";', $tpl);
			}
			else if ($type == 'image')
				$tpl = str_replace('##select##', '$retour["'.$champ.'"]["max"] = 255;' . "\n\t\t" . '$retour["'.$champ.'"]["format"] = "jpg";', $tpl);
			else if ($type == 'file')
				$tpl = str_replace('##select##', '$retour["'.$champ.'"]["max"] = 255;' . "\n\t\t" . '$retour["'.$champ.'"]["format"] = "pdf";', $tpl);
			else
				$tpl = str_replace('##select##', '$retour["'.$champ.'"]["max"] = 255;', $tpl);
			
			$tpl = str_replace('##champ##', $champ, $tpl);
			$tpl = str_replace('##affichage##', $affichage, $tpl);
			$tpl = str_replace('##type##', $type, $tpl);
			$tpl = str_replace('##defaut##', $defaut, $tpl);
			$tpl = str_replace('##js##', $js, $tpl);
			$tpl = str_replace('##tri##', $tri, $tpl);
			$tpl = str_replace('##filtre##', $filtre, $tpl);
			$tpl = str_replace('##obligatoire##', $obligatoire, $tpl);
			
			if ($presentajout == 'true')
				$champs_ajout .= $tpl . "\n\n";
			if ($presentdup == 'true')
				$champs_dup .= $tpl . "\n\n";
			if ($presentmaj == 'true')
				$champs_maj .= $tpl . "\n\n";
			if ($presentliste == 'true')
				$champs_liste .= $tpl . "\n\n";
			if ($presentvue == 'true')
				$champs_vue .= $tpl . "\n\n";
		}
		
		$titre_ajout = $p['titre_ajout'];
		$titre_dup = $p['titre_dup'];
		$titre_maj = $p['titre_maj'];
		$titre_liste = $p['titre_liste'];
		$titre_vue = $p['titre_vue'];
		
		$php_ajout = $p['php_ajout'];
		$php_dup = $p['php_dup'];
		$php_maj = $p['php_maj'];
		$php_liste = $p['php_liste'];
		$php_vue = $p['php_vue'];
		
		//on fait les CRUD
		$add = file_get_contents('tpl/add.tpl');
		$dup = file_get_contents('tpl/dup.tpl');
		$edit = file_get_contents('tpl/edit.tpl');
		$liste = file_get_contents('tpl/liste.tpl');
		$view = file_get_contents('tpl/view.tpl');
		
		$add = str_replace('##champs##', $champs_ajout, $add);
		$dup = str_replace('##champs##', $champs_dup, $dup);
		$edit = str_replace('##champs##', $champs_maj, $edit);
		$liste = str_replace('##champs##', $champs_liste, $liste);
		$view = str_replace('##champs##', $champs_vue, $view);
		
		$add = str_replace('##php##', $php_ajout, $add);
		$dup = str_replace('##php##', $php_dup, $dup);
		$edit = str_replace('##php##', $php_maj, $edit);
		$liste = str_replace('##php##', $php_liste, $liste);
		$view = str_replace('##php##', $php_vue, $view);
		
		$add = str_replace('##titre##', $titre_ajout, $add);
		$dup = str_replace('##titre##', $titre_dup, $dup);
		$edit = str_replace('##titre##', $titre_maj, $edit);
		$liste = str_replace('##titre##', $titre_liste, $liste);
		$view = str_replace('##titre##', $titre_vue, $view);
		
		$liste = str_replace('##champ_tri##', $tri_defaut, $liste);
		
		file_put_contents('generated/add_'.$p['table'].'.php', $add);
		file_put_contents('generated/dup_'.$p['table'].'.php', $dup);
		file_put_contents('generated/edit_'.$p['table'].'.php', $edit);
		file_put_contents('generated/liste_'.$p['table'].'.php', $liste);
		file_put_contents('generated/view_'.$p['table'].'.php', $view);
	}
	
	$op = $gp_request -> Get('op');
	
	if ($op == 'crud')
	{
		$table = $gp_request -> Get('table');
		$fields = get_fields($table);
	}
	if ($op == 'makecrud')
	{
		makecrud($_POST);
		die('CRUD généré<p /><a href="'.$_SERVER["PHP_SELF"].'">Autre table</a>');
	}
	
	// Mise en place de l'ActiveRecord
	GP_ActiveRecord::setDefaultDBConnection($gp_db);
	
	// on accroit les perf si possible
	$version = explode('.', PHP_VERSION, 2);
	if($version[0] * 10 + $version[1] < 53)
		set_magic_quotes_runtime(0);
	
	@set_time_limit(0);
	
	$limite_memoire = @ini_get('memory_limit');
	
	if (!empty($limite_memoire))
	{
		$unite = strtolower(substr($limite_memoire, -1, 1));
		$limite_memoire = (int) $limite_memoire;

		if ($unite == 'k')
			$limite_memoire = floor($limite_memoire / 1024);
		else if ($unite == 'g')
			$limite_memoire *= 1024;
		else if (is_numeric($unite))
			$limite_memoire = floor((int) ($limite_memoire . $unite) / 1048576);
		$limite_memoire = max(128, $limite_memoire) . 'M';
	}
	else
		$limite_memoire = '128M';
	@ini_set('memory_limit', $limite_memoire);
	
	$q = "SHOW TABLES";
	$res = $gp_db -> fetchArray($q);
	$alltables = array();
	foreach ($res as $tablet => $tab)
		foreach ($tab as $c => $v)
			$alltables[] = $v;
?>
<html>
	<head>
		<title>CRUD</title>
		<style>
			*
			{
				font-family: verdana;
				font-size: 11px;
			}
		</style>
	</head>
	<body>
	<center><h1>CRUD GENERATION</h1></center>
	<p />
	<table style="width: 100%; border: none;" cellpadding="5" cellspacing="0">
	<tr>
		<td align="center">TABLES</td>
	</tr>
	<?php foreach($alltables as $t): ?>
	<tr>
		<td><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?op=crud&table=<?php echo $t; ?>"><?php echo $t; ?></a></td>
	</tr>
	<?php endforeach; ?>
	</table>
	<?php if($op == 'crud'): ?>
	<p />
	<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
	<fieldset>
		<input type="hidden" id="op" name="op" value="makecrud" />
		<input type="hidden" id="table" name="table" value="<?php echo $table; ?>" />
		<table style="width: 100%; border: none;" cellpadding="5" cellspacing="0">
		<tr>
			<td colspan="17" align="center" style="font-weight: bold;">CHAMPS</td>
		</tr>
		<tr style="font-weight: bold;">
			<td>&nbsp;</td>
			<td>Présent Ajout</td>
			<td>Présent Dup</td>
			<td>Présent MAJ</td>
			<td>Présent Liste</td>
			<td>Présent Vue</td>
			<td>ID</td>
			<td>Affichage</td>
			<td>Type</td>
			<td>Type select</td>
			<td>Data</td>
			<td>Premier choix</td>
			<td>Défaut</td>
			<td>JS</td>
			<td>Obligatoire</td>
			<td>Tri</td>
			<td>Défaut Tri</td>
			<td>Filtre</td>
		</tr>
		<?php $i = 1; ?>
		<?php foreach($fields as $f): ?>
		<tr>
			<td>
				<strong><?php echo $f; ?></strong>
			</td>
			<td>
				<select name="presentajout_<?php echo $i; ?>" id="presentajout_<?php echo $i; ?>">
					<option value="true">Oui</option>
					<option value="false">Non</option>
				</select>
			</td>
			<td>
				<select name="presentdup_<?php echo $i; ?>" id="presentadup_<?php echo $i; ?>">
					<option value="true">Oui</option>
					<option value="false">Non</option>
				</select>
			</td>
			<td>
				<select name="presentmaj_<?php echo $i; ?>" id="presentmaj_<?php echo $i; ?>">
					<option value="true">Oui</option>
					<option value="false">Non</option>
				</select>
			</td>
			<td>
				<select name="presentliste_<?php echo $i; ?>" id="presentliste_<?php echo $i; ?>">
					<option value="true">Oui</option>
					<option value="false">Non</option>
				</select>
			</td>
			<td>
				<select name="presentvue_<?php echo $i; ?>" id="presentvue_<?php echo $i; ?>">
					<option value="true">Oui</option>
					<option value="false">Non</option>
				</select>
			</td>
			<td>
				<input name="id_<?php echo $i; ?>" id="id_<?php echo $i; ?>" value="<?php echo $f; ?>" /></td>
			<td>
				<input name="affichage_<?php echo $i; ?>" id="affichage_<?php echo $i; ?>" value="<?php echo $f; ?>" />
			</td>
			<td>
				<select name="type_<?php echo $i; ?>" id="type_<?php echo $i; ?>">
					<option value="input">input</option>
					<option value="select">select</option>
					<option value="textarea">textarea</option>
					<option value="image">image</option>
					<option value="fichier">fichier</option>
					<option value="date">date</option>
					<option value="password">password</option>
					<option value="html">html</option>
					<option value="radio">radio</option>
					<option value="checkbox">checkbox</option>
				</select>
			</td>
			<td>
				<select name="typeselect_<?php echo $i; ?>" id="typeselect_<?php echo $i; ?>">
					<option value="vocabulaire">vocabulaire</option>
					<option value="sql">sql</option>
				</select>
			</td>
			<td>
				<input name="data_<?php echo $i; ?>" id="data_<?php echo $i; ?>" value="" />
			</td>
			<td>
				<input name="premierchoix_<?php echo $i; ?>" id="premierchoix_<?php echo $i; ?>" value="" />
			</td>
			<td>
				<input name="defaut_<?php echo $i; ?>" id="defaut_<?php echo $i; ?>" value="" />
			</td>
			<td>
				<input name="js_<?php echo $i; ?>" id="js_<?php echo $i; ?>" value="return false;" />
			</td>
			<td>
				<select name="obligatoire_<?php echo $i; ?>" id="obligatoire_<?php echo $i; ?>">
					<option value="true">Oui</option>
					<option value="false">Non</option>
				</select>
			</td>
			<td>
				<select name="tri_<?php echo $i; ?>" id="tri_<?php echo $i; ?>">
					<option value="true">Oui</option>
					<option value="false">Non</option>
				</select>
			</td>
			<td>
				<select name="defauttri_<?php echo $i; ?>" id="defauttri_<?php echo $i; ?>">
					<option value="false">Non</option>
					<option value="true">Oui</option>
				</select>
			</td>
			<td>
				<select name="filtre_<?php echo $i; ?>" id="filtre_<?php echo $i; ?>">
					<option value="true">Oui</option>
					<option value="false">Non</option>
				</select>
			</td>
		</tr>
		<?php ++$i; ?>
		<?php endforeach; ?>
		</table>
		<p />
		<table style="width: 100%; border: none;" cellpadding="5" cellspacing="0">
		<tr style="font-weight: bold;">
			<td>&nbsp;</td>
			<td>Page Ajout</td>
			<td>Page Dup</td>
			<td>Page MAJ</td>
			<td>Page Liste</td>
			<td>Page Vue</td>
		</tr>
		<tr>
			<td>PHP</td>
			<td>
				<textarea name="php_ajout" id="php_ajout" style="width:150px; height: 75px;"></textarea>
			</td>
			<td>
				<textarea name="php_dup" id="php_dup" style="width:150px; height: 75px;"></textarea>
			</td>
			<td>
				<textarea name="php_maj" id="php_maj" style="width:150px; height: 75px;"></textarea>
			</td>
			<td>
				<textarea name="php_liste" id="php_liste" style="width:150px; height: 75px;"></textarea>
			</td>
			<td>
				<textarea name="php_vue" id="php_vue" style="width:150px; height: 75px;"></textarea>
			</td>
		</tr>
		<tr>
			<td>Titre</td>
			<td>
				<input name="titre_ajout" id="titre_ajout" value="Ajouter un " />
			</td>
			<td>
				<input name="titre_dup" id="titre_dup" value="Dupliquer un " />
			</td>
			<td>
				<input name="titre_maj" id="titre_maj" value="Editer un " />
			</td>
			<td>
				<input name="titre_liste" id="titre_liste" value="Liste des " />
			</td>
			<td>
				<input name="titre_vue" id="titre_vue" value="Afficher un " />
			</td>
		</tr>
		</table>
		<p />
		<input type="submit" value="OK" />
	</fieldset>
	</form>
	<?php endif; ?>
	</body>
</html>